package rd.hardcore.engine.datas 
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import rd.hardcore.datas.Track;
	import rd.hardcore.engine.datas.streamBoard.StreamBoardEvent;
	import rd.hardcore.engine.datas.streamBoard.StreamBoardStateEvent;
	import rd.hardcore.engine.datas.streamBoard.StreamBoardSynchroEvent;
	import rd.hardcore.params.GlobalParams;
	/**
	 * ...
	 * @author Fazermokeur
	 */
	public class StreamBoardData extends EventDispatcher
	{
		private const azerty_keyCode				: Array = [65, 90, 69, 82, 84, 89, 85, 73, 79, 80, 81, 83, 68, 70, 71, 72, 74, 75, 76, 77, 87, 88, 67, 86, 66, 78];
		private const qwerty_keyCode				: Array = [81, 87, 69, 82, 84, 89, 85, 73, 79, 80, 65, 83, 68, 70, 71, 72, 74, 75, 76, 186, 90, 88, 67, 86, 66, 78];
		
		private var _navMediaURL	: String;
		private var _id 			: String;
		private var _labelName		: String;
		private var _activate		: Boolean;
		private var _vPads			: Vector.<Track>;
		private var _state			: String;
		private var _background		: String;
		private var _preview		: String;
		private var t:uint;
		
		/*
		 * <board id=\"" + sbd.id + "\" state=\"" + sbd.active + "\" name=\"" + sbd.labelName + "\"><![CDATA[" + sbd.NavMedia + "]]></board>
		 */
		public function StreamBoardData(id:String, name:String) 
		{
			_vPads = new Vector.<Track>();
			_state = StreamBoardManager.PENDING;
			_id = id;
			_labelName = name;
			_activate = true;
		}
		
		public function buildDatas(e:StreamBoardSynchroEvent):void 
		{
			trace("0:StreamBoardManager::B U I L D D A T A S");
			
			var fs:FileStream = new FileStream;
			var tracksFile:File = File.applicationStorageDirectory.resolvePath("boards/" + e.name + "/" + e.name + ".xml");
			fs.open(tracksFile, FileMode.READ);
			var tracksXmlString:String = fs.readUTFBytes(fs.bytesAvailable);
			var tracksXMLList:XMLList = new XMLList(tracksXmlString);
			var tracksXML:XML = tracksXMLList.tracks[0];
			
			_navMediaURL = "boards/" + e.name + "/" + tracksXMLList.navMedia.toString();
			_background = "boards/" + e.name + "/" + tracksXMLList.backgroundMedia.toString(); 
			_preview	= "boards/" + e.name + "/" + tracksXMLList.previewMedia.toString(); 
			
			fs.close();
			
			var t:Track;
			var node:XML;
			for each (node in tracksXML.track) 
			{
				t = new Track();
				t.build(node);
				if (t.thumb) t.thumb = "boards/" + e.name + "/" + t.thumb;
				/*else {
					t.thumb = "medias/thumbs/empty.jpg";
				}*/
				if (t.file) t.file  = "boards/" + e.name + "/" + t.file;
				/*else {
					t.file = "medias/songs/empty.mp3";
				}*/
				_vPads.push(t);
			}
			
			trace("0:_STATE = STREAM_BOARD_READY");
			_state = StreamBoardManager.STREAM_BOARD_READY;
			dispatchEvent(new StreamBoardEvent(StreamBoardEvent.BOARD_READY_TO_DISPLAY));
		}
		
		public function getPadManagerNode(uid:int):XML
		{
			var keyMapref:Array = (GlobalParams.AZERTY_MODE)?azerty_keyCode:qwerty_keyCode;
			
			var len:int = _vPads.length;
			var datas:String = "<padmanager id=\""+uid+"\">"+"\n";
			var t:Track;
			
			for (var i:int = 0; i < len; i++) 
			{
				t = _vPads[i];
				t.keyId += uid*26;
				datas = datas + "<pad keyId=\"" + t.keyId +"\" pos=\"11\" hit=\"" + t.cpt + "\"><![CDATA[" + keyMapref[i] + "]]></pad>" + "\n";
			}
			datas = datas + "</padmanager>";
			
			return new XML(datas);
		}
		
		/**
		 * Génération du XML pour écriture dans tracks.xml
		 * @return	contenu du noeud data
		 */
		public function getTracksXML():XML
		{
			var node:XML = <tracks/>;
			var len:int = _vPads.length;
			var t:Track;
			for (var i:int = 0; i < len; i++) 
			{
				t = _vPads[i];
				node.appendChild(t.genChild());
			}
			return node;
		}
		
		public function getTrackById(keyId:int):Track
		{
			var len:int = _vPads.length;
			var t:Track;
			for (var i:int = 0; i < len; i++) 
			{
				if (_vPads[i].keyId == keyId)	return _vPads[i];
			}
			return null;
		}
		
		public function get navMediaURL():String { return _navMediaURL; }
		
		public function get active():Boolean { return _activate; }
		
		public function set active(value:Boolean):void 
		{
			clearTimeout(t);
			_activate = value;
			//	retarde le ré-ordering de StreamBoardData
			t = setTimeout(dispatch, 100);
		}
		
		private function dispatch():void
		{
			dispatchEvent(new StreamBoardStateEvent(StreamBoardStateEvent.STATE_CHANGE, true));
		}
		
		public function get id():String { return _id; }
		
		public function set id(value:String):void 
		{
			_id = value;
		}
		
		public function get labelName():String { return _labelName; }
		
		public function set labelName(value:String):void 
		{
			_labelName = value;
		}
		
		public function get state():String { return _state; }
		
		public function set state(value:String):void 
		{
			_state = value;
		}
		
		public function get background():String { return _background; }
		
		public function get preview():String { return _preview; }
		
	}

}